package com.yunche.yunchebusiness.ui.activity.brand

import android.content.Intent
import android.support.v4.widget.SwipeRefreshLayout
import android.support.v7.widget.LinearLayoutManager
import android.view.View
import com.chad.library.adapter.base.BaseQuickAdapter
import com.google.gson.JsonArray
import com.yunche.yunchebusiness.R
import com.yunche.yunchebusiness.base.BaseActivity
import com.yunche.yunchebusiness.common.Constant
import com.yunche.yunchebusiness.entity.Brand
import com.yunche.yunchebusiness.entity.BrandInfoEntity
import com.yunche.yunchebusiness.mvp.contract.BrandContract
import com.yunche.yunchebusiness.mvp.presenter.BrandPresenter
import com.yunche.yunchebusiness.ui.adapter.BrandAdapter
import kotlinx.android.synthetic.main.activity_brand.*

class BrandActivity : BaseActivity(), BrandContract.View, SwipeRefreshLayout.OnRefreshListener,
    BaseQuickAdapter.OnItemClickListener {
    var barIndex = hashMapOf<String, Int>()
    var brands = arrayListOf<Brand>()
    var indexs = ""
    var count = 0
    private var mAdapter: BrandAdapter? = null

    private val mPresenter: BrandPresenter by lazy {
        BrandPresenter()
    }



    override fun getBrandSuccess(result: JsonArray) {
        msv_brand.showContent()
        barIndex.clear()
        brands.clear()
        count = 0
        indexs = ""
        var brand = result

        for (position in brand.withIndex()) {

            var datas = brand.get(position.index).asJsonObject

            for (key in datas.keySet()) {
                indexs += key
            }

            side_bar.setIndexItems(indexs.split(""))

            var aeles = datas.get(indexs[position.index].toString()).asJsonArray

            for (aele in aeles) {
                count++
                var brandEle = aele.asJsonObject
                var brand = Brand()
                brand.name = brandEle.get("name").asString
                brand.code = brandEle.get("code").asString
                brand.id = brandEle.get("id").asInt
                brand.initial = brandEle.get("initial").asString
                brand.icon = brandEle.get("icon").asString
                brands.add(brand)
            }
            if (!barIndex.containsKey(aeles.get(0).asJsonObject.get("initial").asString)) {
                barIndex[aeles.get(0).asJsonObject.get("initial").asString] = count - aeles.size() + position.index

                var brand = Brand()
                brand.type = 1
                brand.initial = aeles.get(0).asJsonObject.get("initial").asString
                brands.add(count - aeles.size() + position.index, brand)

            }
        }
        if (mAdapter == null) {
            mAdapter = BrandAdapter(brands)
            rv_brand.adapter = mAdapter
            mAdapter!!.openLoadAnimation(BaseQuickAdapter.SCALEIN)
            (mAdapter as BrandAdapter).onItemClickListener = this
        }
        side_bar.setOnSelectIndexItemListener {
            var index = barIndex[it]
            if (index != null) {
                rv_brand.scrollToPosition(index)
            }
        }
    }


    override fun showLoading() {
        msv_brand.showLoading()
        srl_brand.isRefreshing = true
    }

    override fun hideLoading() {
        srl_brand.isRefreshing = false

    }

    override fun showError(msg: String) {
        msv_brand.showError(msg)
    }

    override fun onRefresh() {
        initData()
    }

    override fun onItemClick(adapter: BaseQuickAdapter<*, *>?, view: View?, position: Int) {

        var intent = Intent(mContext, SeriesActivity::class.java)
        var carInfoEntity = BrandInfoEntity()
        carInfoEntity.brandId = "${brands[position].id}"
        carInfoEntity.brandName = brands[position].name
        carInfoEntity.brandCode = brands[position].code
        carInfoEntity.brandIcon = brands[position].icon
        intent.putExtra(Constant.BRAND_INFO_ENTITY, carInfoEntity)
        startActivityForResult(intent, Constant.SELECT_MODEL_RESULT_CODE)
    }

    override fun setLayout(): Int = R.layout.activity_brand

    override fun initView() {
        mPresenter.attachView(this)
        rv_brand.layoutManager = LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false)
        srl_brand.setOnRefreshListener(this)
        barIndex.clear()
        barIndex["A"] = 0
        var brand = Brand()
        brand.type = 1
        brand.initial = "A"
        brands.add(0, brand)

        msv_brand.setOnRetryClickListener {
            initData()
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        when (resultCode) {
            Constant.SELECT_MODEL_RESULT_CODE -> {
                var intent = Intent()
                intent.putExtras(data)
                setResult(Constant.SELECT_MODEL_RESULT_CODE, intent)
                finish()
            }
        }
    }

    override fun initData() {
        mPresenter.getBrand()
    }

    override fun initEvent() {
    }


    override fun onDestroy() {
        super.onDestroy()
        mPresenter.detachView()
    }
}
